<?php
    namespace Npfe\Controller;
    use think\Controller;

    class GoodsController extends Controller{
        public function index($mode = 'exchange'){
            switch($mode){
                case 'exchange':
                    $stt['listroadter'] = 1;
                    $stt['addbutton'] = 'hjadd';
                    $stt['title'] = '售后处理-换货';
                    $stt['searchbutton'] = 'checkhjsubmit()';
                    break;
                case 'return':
                    $stt['listroadter'] = 2;
                    $stt['addbutton'] = 'tjadd';
                    $stt['title'] = '售后处理-退货';
                    $stt['searchbutton'] = 'checktjsubmit()';
                    break;
                default:
                    $stt['listroadter'] = 0;
                    $stt['title'] = '无法获取';
                    break;
            }
            $this->assign($stt);
            $this->display();
        }
        function hjadd(){
            $this->display();
        }
        function tjadd(){
            $this->display();
        }
        function loadhjtp($link = '') {
            $stt = array(
                'ctldate' => date('Y-m-d',time()),
                'orderdate' => '<input class="easyui-datetimebox" name="orderdate" id="orderdate" style="width:100px" />',
                'snid' => '<input class="easyui-textbox" type="text" name="snid" id="hjtpsnid" data-options="required:true" style="width:80%">',
                'shixian' => '<select class="easyui-combobox" name="shixian"><option value="7天内">7天内</option><option value="7至15天内">7至15天内</option><option value="超过15天">超过15天</option></select>',
                'liyou' => '<select class="easyui-combobox" name="liyou"><option value="产品质量">产品质量</option><option value="销售误导">销售误导</option><option value="同价异款">同价异款</option><option value="异价异款">异价异款</option></select>',
                'cnumber' => '<input class="easyui-textbox" type="text" name="cnumber" data-options="required:true">',
                'cname' => '<input class="easyui-textbox" type="text" name="cname" data-options="required:true">',
                'o_cellphone' => '<input class="easyui-textbox" type="text" name="o_cellphone" data-options="required:true">',
                'o_sn' => '<input class="easyui-textbox" type="text" name="o_sn" data-options="required:true">',
                'n_cellphone' => '<input class="easyui-textbox" type="text" name="n_cellphone" data-options="required:true">',
                'n_sn' => '<input class="easyui-textbox" type="text" name="n_sn" data-options="required:true">',
                'n_address' =>'<input class="easyui-textbox" type="text" name="n_address" data-options="required:true" style="width:90%">',
                'fanying' => '<textarea name="fanying" id="fanying" style="margin: 0px;width:100%;height:200px;"></textarea>',
                'chajia' => '<input class="easyui-textbox" type="text" name="chajia" data-options="required:true">',
                'shoukuaidi' => '<input class="easyui-textbox" type="text" name="shoukuaidi" data-options="required:true">',
                'jikuaidi' => '<input class="easyui-textbox" type="text" name="jikuaidi" data-options="required:true">',
                'msc' => '打印或预览时生成',
                'mss' => '打印或预览时生成',
                'msj' => '打印或预览时生成',
            );
            if (!empty($link)) {
                $glxq = new \Npfe\Model\GlxqModel();
                $noc = new \Npfe\Model\NocModel();
                $data = $glxq->where("contactnumber = '$link'")->find();
                if ($data != false) {
                    $info = array(
                        'cname' => $data['customername'],
                        'fanying' => $data['content']
                    );            
                }else {
                    $data = $noc->where("cnumber = '$link'")->find();
                    if ($data != false) {
                        $info = array(
                            'cname' => $data['cname'],
                            'fanying' => $data['fanying']
                        );
                    }
                }
                $stt['cnumber'] = '<input class="easyui-textbox" type="text" name="cnumber" data-options="required:true" value="'.$link.'" />';
                $stt['cname'] = '<input class="easyui-textbox" type="text" name="cname" data-options="required:true" value="'.$info['cname'].'" />';
                //$stt['fanying'] = '<textarea name="fanying" id="fanying" style="margin: 0px;width:100%;height:200px;">'.$info['fanying'].'</textarea>';
                $stt['fanying'] = '<script id="fanying" name="fanying" type="text/plain" style="margin: 0px;width:100%;height:200px;">'.$info['fanying'].'</script>';
            }
            $stt['ctlman'] = session('nickname');
            $this->assign($stt);
            $this->display('npfe_thj_hjd');
        }
        function loadtjtp($link = NULL){
            $now_em = session('nickname');
            $tp = file_get_contents('./npfe_thj_tjd.html');
            $stt = array(
                'sman' => '<input class="easyui-textbox" type="text" name="sman" id="sman" data-options="required:true" style="width:80%">',
                'orderdate' => '<input class="easyui-datetimebox" name="orderdate" id="orderdate" style="width:100px" />',
                'snid' => '<input class="easyui-textbox" type="text" name="snid" id="tjtpsnid" data-options="required:true" style="width:80%">',
                'shixian' => '<select class="easyui-combobox" name="shixian"><option value="7天内">7天内</option><option value="7至15天内">7至15天内</option><option value="超过15天">超过15天</option></select>',
                'liyou' => '<select class="easyui-combobox" name="liyou"><option value="产品质量">产品质量</option><option value="销售误导">销售误导</option><option value="同价异款">同价异款</option><option value="异价异款">异价异款</option></select>',
                'cnumber' => '<input class="easyui-textbox" type="text" name="cnumber" data-options="required:true">',
                'cname' => '<input class="easyui-textbox" type="text" name="cname" data-options="required:true">',
                'o_cellphone' => '<input class="easyui-textbox" type="text" name="o_cellphone" data-options="required:true">',
                'o_sn' => '<input class="easyui-textbox" type="text" name="o_sn" data-options="required:true">',
                'n_cellphone' => '<input class="easyui-textbox" type="text" name="n_cellphone" data-options="required:true">',
                'n_sn' => '<input class="easyui-textbox" type="text" name="n_sn" data-options="required:true">',
                'n_address' =>'<input class="easyui-textbox" type="text" name="n_address" data-options="required:true" style="width:90%">',
                'fanying' => '<input class="easyui-textbox" name="fanying" data-options="multiline:true" style="width:100%;height:200px;">',
                'jieshi' => '<input class="easyui-textbox" name="jieshi" data-options="multiline:true" style="width:100%;height:200px;">',
                'chajia' => '<input class="easyui-textbox" type="text" name="chajia" data-options="required:true">',
                'shoukuaidi' => '<input class="easyui-textbox" type="text" name="shoukuaidi" data-options="required:true">',
                'jikuaidi' => '<input class="easyui-textbox" type="text" name="jikuaidi" data-options="required:true">',
                'msc' => '打印或预览时生成',
                'mss' => '打印或预览时生成',
                'msj' => '打印或预览时生成',
            );
            if (!empty($link)) {
                $glxq = new \Npfe\Model\GlxqModel();
                $noc = new \Npfe\Model\NocModel();
                $data = $glxq->where("contactnumber = '$link'")->find();
                if ($data != false) {
                    $info = array(
                        'cname' => $data['customername'],
                        'cnumber' => $link,
                        'fanying' => $data['content']
                    );            
                }else {
                    $data = $noc->where("cnumber = '$link'")->find();
                    if ($data != false) {
                        $info = array(
                            'cname' => $data['cname'],
                            'cnumber' => $link,
                            'fanying' => $data['fanying']
                        );
                    }
                }
            }

            $stt['cnumber'] = '<input class="easyui-textbox" type="text" name="cnumber" data-options="required:true" value="'.$link.'" />';
            $stt['cname'] = '<input class="easyui-textbox" type="text" name="cname" data-options="required:true" value="'.$info['cname'].'" />';
            $stt['fanying'] = '<input class="easyui-textbox" name="fanying" data-options="multiline:true" value="'.strip_tags($info['fanying']).'" style="width:100%;height:200px;"/>'; 

            $stt['ctldate'] = date('Y-m-d',time());
            $stt['ctlman'] = $now_em;
            $this->assign($stt);
            $this->display('npfe_thj_tjd');
        }
        function listthj($mode = 1){
            $items = array();
            $dmt = \Npfe\Controller\MainController::getdmt();
            $thj = new \Npfe\Model\ThjModel();
            switch ($mode) {
                case 1:
                    $data = $thj->where(" type = '换机' && ctldate >= '$dmt[dmtb]' and ctldate <= '$dmt[dmte]'")->select();
                    if (!empty($_POST['lxnumber'])) {
                        $cond = array(
                            'type' => '换机',
                            'cnumber' => I('post.lxnumber')
                        );
                        $data = $thj->where($cond)->select();
                    }elseif (!empty($_POST['starttime'])) {
                        $formated = \Npfe\Controller\MainController::formattime($_POST['starttime'], $_POST['endtime']);
                        $data = $thj->where(" type = '换机' && ctldate >= '$formated[stt]' && ctldate <= '$formated[ett]'")->select();
                    };
                    break;
                case 2:
                    $data = $thj->where(" type = '退机' && ctldate >= '$dmt[dmtb]' and ctldate <= '$dmt[dmte]'")->select();
                    if (!empty($_POST['lxnumber'])) {
                        $cond = array(
                            'type' => '退机',
                            'cnumber' => I('post.lxnumber')
                        );
                        $data = $thj->where($cond)->select();
                    }elseif (!empty($_POST['starttime'])) {
                        $formated = \Npfe\Controller\MainController::formattime($_POST['starttime'], $_POST['endtime']); 
                        $data = $thj->where(" type = '退机' && ctldate >= '$formated[stt]' && ctldate <= '$formated[ett]'")->select();
                    };
                    break;
            }

            foreach($data as $row){
                array_push($items, $row);
            }
            $value =array(
                "total" => count($items),
                'rows' => $items
            );
            $nonetr = json_encode($value);
            echo $nonetr;
        }
        function addthjc($mode = 1){
            $now_em = session('nickname');
            $orderdate = date('Y-m-d H:i:s',strtotime(I('post.orderdate')));
            $cn = I('post.cnumber');
            $thj = new \Npfe\Model\ThjModel();
            $pg = new \Npfe\Model\PgModel();
            switch($mode){
                case 1:
                    $data = array(
                        'snid' => $_POST['snid'],
                        'cname'=> $_POST['cname'],
                        'cnumber' => $cn,
                        'type' => '换机',
                        'orderdate' => $orderdate,
                        'ctlman' => $now_em,
                        'ctldate' => date('Y-m-d H:i:s',time()),
                        'shixian' => $_POST['shixian'],
                        'liyou' => $_POST['liyou'],
                        'fanying' => $_POST['fanying'],
                        'jieshi' => NULL,
                        'o_cellphone' => $_POST['o_cellphone'], 
                        'o_sn' => $_POST['o_sn'],
                        'n_cellphone' => $_POST['n_cellphone'],
                        'n_sn' => $_POST['n_sn'],
                        'o_cn' => NULL,
                        'n_cn' => NULL,
                        'bz' => NULL,
                        'jikuaidi' => $_POST['jikuaidi'],
                        'shoukuaidi' => $_POST['shoukuaidi'],
                        'chajia' => $_POST['chajia'],
                        'tuikuan'=> NULL,
                        'o_address' => NULL,
                        'n_address' => $_POST['n_address']
                    );
                    break;
                case 2:
                    $data = array(
                        'snid' => $_POST['snid'],
                        'cname'=> $_POST['cname'],
                        'cnumber' => $cn,
                        'type' => '退机',
                        'orderdate' => $orderdate,
                        'ctlman' => $now_em,
                        'ctldate' => date('Y-m-d H:i:s',time()),
                        'shixian' => $_POST['shixian'],
                        'fanying' => $_POST['fanying'],
                        'jieshi' => NULL,
                        'o_cellphone' => $_POST['o_cellphone'], 
                        'o_sn' => $_POST['o_sn'],
                        'n_cellphone' => $_POST['n_cellphone'],
                        'n_sn' => $_POST['n_sn'],
                        'o_cn' => NULL,
                        'n_cn' => NULL,
                        'bz' => NULL,
                        'jikuaidi' => $_POST['jikuaidi'],
                        'shoukuaidi' => $_POST['shoukuaidi'],
                        'chajia' => $_POST['chajia'],
                        'tuikuan'=> NULL,
                        'o_address' => NULL,
                        'n_address' => $_POST['n_address'],
                        'sman' =>  $_POST['sman']
                    );
                    break;
            }
            try {
                $res = $thj->add($data);
                if ($res > 0) {
                    $pg->modiflypg($cn, $_POST['snid'], 'c');
                    echo "系统已成功添加纪录！";
                }
            } catch (Exception $e) {
                if ($e->g=="") {
                    print $e->getMessage();
                }
            }
        }

        function viewrfs($snid,$time){
            $thj = new \Npfe\Model\ThjModel();
            $row = $thj->where("snid='$snid' && ctldate='$time'")->find();
            if ($row != false){
                switch ($row['type']) {
                    case '换机':
                        $row['msc'] = $thj->getMoneyString($row['chajia']);
                        $row['mss'] = $thj->getMoneyString($row['shoukuaidi']);
                        $row['msj'] = $thj->getMoneyString($row['jikuaidi']);
                        $this->assign($row);
                        $this->display('npfe_thj_hjd');
                        break;
                    case '退机':
                        $row['msc'] = $thj->getMoneyString($row['chajia']);
                        $row['mss'] = $thj->getMoneyString($row['shoukuaidi']);
                        $row['msj'] = $thj->getMoneyString($row['jikuaidi']);
                        $this->assign($row);
                        $this->display('npfe_thj_tjd');
                        break;

                    default:
                        reviewhjc($snid);
                        break;
                }
            }

        }
        function review($mode = 1){
            $now_em = session('nickname');
            $orderdate = date('Y-m-d H:i:s',strtotime(I('post.orderdate')));
            $cn = I('post.cnumber');
            switch($mode){
                case 1:
                    $data = array(
                        'snid' => $_POST['snid'],
                        'cname'=> $_POST['cname'],
                        'cnumber' => $cn,
                        'type' => '换机',
                        'orderdate' => $orderdate,
                        'ctlman' => $now_em,
                        'ctldate' => date('Y-m-d H:i:s',time()),
                        'shixian' => $_POST['shixian'],
                        'liyou' => $_POST['liyou'],
                        'fanying' => $_POST['fanying'],
                        'jieshi' => NULL,
                        'o_cellphone' => $_POST['o_cellphone'], 
                        'o_sn' => $_POST['o_sn'],
                        'n_cellphone' => $_POST['n_cellphone'],
                        'n_sn' => $_POST['n_sn'],
                        'o_cn' => NULL,
                        'n_cn' => NULL,
                        'bz' => NULL,
                        'jikuaidi' => $_POST['jikuaidi'],
                        'shoukuaidi' => $_POST['shoukuaidi'],
                        'chajia' => $_POST['chajia'],
                        'tuikuan'=> NULL,
                        'o_address' => NULL,
                        'n_address' => $_POST['n_address']
                    );
                    $this->assign($data);
                    $this->display('npfe_thj_hjd');
                    break;
                case 2:
                    $data = array(
                        'snid' => $_POST['snid'],
                        'cname'=> $_POST['cname'],
                        'cnumber' => $cn,
                        'type' => '退机',
                        'orderdate' => $orderdate,
                        'ctlman' => $now_em,
                        'ctldate' => date('Y-m-d H:i:s',time()),
                        'shixian' => $_POST['shixian'],
                        'fanying' => $_POST['fanying'],
                        'jieshi' => NULL,
                        'o_cellphone' => $_POST['o_cellphone'], 
                        'o_sn' => $_POST['o_sn'],
                        'n_cellphone' => $_POST['n_cellphone'],
                        'n_sn' => $_POST['n_sn'],
                        'o_cn' => NULL,
                        'n_cn' => NULL,
                        'bz' => NULL,
                        'jikuaidi' => $_POST['jikuaidi'],
                        'shoukuaidi' => $_POST['shoukuaidi'],
                        'chajia' => $_POST['chajia'],
                        'tuikuan'=> NULL,
                        'o_address' => NULL,
                        'n_address' => $_POST['n_address'],
                        'sman' =>  $_POST['sman']
                    );
                    $this->assign($data);
                    $this->display('npfe_thj_tjd');
                    break;
            }
        }
    }
?>
